new Vue({
    el:'#main',
    data:()=>({
        admin_id:0,
        requestUrl:'',
        requestType:'list',
        page:1,
        pageSize:10,
        pageSum:0,
        id:1,
        list:[],
        viewPage:0,
        pickuping:[],   // 提货中
        pauseing:[],    // 暂停中
        finish:[],      // 已完成
        cancel:[],      // 取消
        hasTask:0,
        pickupDetail:{
            proInfo:{},
            storeInfo:{
                store:{},
                detail:[],
            }
        },
        pickupDetailRightList:[],
        pickupData:{},
        pickupQtyCount:{    // 开始提货 合计提货数量
            prochar_qty:0,
            qty:0
        },
        pickupingCheck:{
            proInfo:{},
            storeInfo:{
                store:{},
                detail:[],
            },
            right:[]
        },
        pickupingInputMaxPcs:0,
        placeList:[],
        status:false,
        pickupMissingData:{
            proInfo:{},
            storeInfo:{
                store:{},
                detail:[]
            },
            saleList:[],
            qtySum:0,
            pickupMissing:0
        },
        isdisabled:false,
        auth_ids:$('#adminauth_ids').text(),
        navPromptNum:{
            listNum:0,
            pickupingNum:0,
            pauseingNum:0,
            finishNum:0,
            cancelNum:0,
        }
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            $('.loding').show()
            var data={
                requestType:this.requestType,
                page:this.page,
                pageSize:this.pageSize,
                is_me:this.status,
            }
            $.ajax({
                url:this.requestUrl,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this[this.requestType] = []
                    if(data.list != undefined){
                        this[this.requestType] = data.list != undefined ?  data.list : []
                        
                    }else{
                        this[this.requestType] = []
                    }
                    this.viewPage = this[this.requestType].length
                    this.hasTask = data.hasTask
                    this.pageSum = data.pageSum
                    if(data.pageSum){
                        this.Resetnav(data.page-0,data.pageSum)
                    }
                    this.navPageNum()
                    $('.loding').hide()
                }
            })
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        clickPickup(type,index,key){
            this.pickupDetailRightList = [];
            this.pickupQtyCount.prochar_qty = 0;
            this.pickupQtyCount.qty = 0;
            if(type == 1){
                this.pickupDetail = this.list[index][key]
                this.pickupDetailRightList.push(this.list[index][key])
                this.pickupQtyCount.prochar_qty = this.pickupDetail.proInfo.prochar_qty
                this.pickupQtyCount.qty = this.pickupDetail.qty
            }
            if(type == 2){
                var detailKey = []
                var prochar_id = [] // 便利 判断选择的产品是否为同一产品
                var detail = []
                $("input[name='pickupDetailCheck']:checked").each(function(i){//把所有被选中的复选框的值存入数组
                    detailKey[i] = {listKey:0,detailKey:0,};
                    detailKey[i].detailKey = $(this).val()
                    detailKey[i].listKey = $(this).attr('listKey')

                    prochar_id[i] = $(this).attr('prochar_id')
                });


                if(detailKey.length > 0){
                    // 判断选择的产品是否一致
                    var nary=prochar_id.sort();
                    for(var i=0;i<nary.length;i++){
                        if(i < nary.length-1 ){
                            if (nary[i]!=nary[i+1]){
                                var data = {status:2,msg:'请确保选择提货的产品统一性'}
                                this.point(data,'#mainalert')
                                return false
                            }    
                        }
                    }

                    $('#pickup_startModal').modal('toggle')
                    var inArrayData = [];
                    for (var j = 0; j < detailKey.length; j++) {
                        var data = this.list[detailKey[j]['listKey']][detailKey[j]['detailKey']];
                        this.pickupDetail = data
                        // 判断是否为同i销售单对的同一产品
                        if(data.sale_id > 0 && $.inArray(data.sale_id,inArrayData) >= 0){

                        }else if(data.sale_id < 0 || $.inArray(data.sale_id,inArrayData) < 0){
                            this.pickupDetailRightList.push(data)
                            if(data.sale_id > 0) inArrayData.push(data.sale_id)
                            this.pickupQtyCount.prochar_qty = data.proInfo.prochar_qty
                            this.pickupQtyCount.qty += data.qty
                        }
                    }
                    this.getdata()
                }else{
                    $('#pickup_startModal').hide()
                    var data = {status:2,msg:'请选择需要提货的产品'}
                    this.point(data,'#mainalert')
                }
                
            }
        },
        savePickupDetail(){
            this.isdisabled = true
            var data = {
                list:this.pickupDetailRightList
            }
            $.ajax({
                url:'/admin/saledocker/savePickupDetail',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.point(data,'#pickup_startModal')
                    if(data.status == 1){
                        this.point(data,'#mainalert',2)
                        this.getdata()
                    } 
                    this.isdisabled = false
                }
            }) 

        },
        // 查看
        clickCheck(index,dataName){
            this.pickupingCheck.proInfo = this[dataName][index]['detail'][0].proInfo
            this.pickupingCheck.storeInfo = this[dataName][index]['detail'][0].storeInfo
            this.pickupingCheck.right = this[dataName][index]
            // this.pickupingInputMax = 
            // 设置当前最大input 值
            this.pickupingInputMaxPcs = this.pickupingCheck.right.qty_sum
            for (var i = 0; i < this.pickupingCheck.storeInfo.detail.length; i++) {
                this.pickupingCheck.storeInfo.detail[i]['inputValue'] = 0
                if(this.pickupingInputMaxPcs == 0 ){
                    this.pickupingCheck.storeInfo.detail[i]['inputValue'] = 0
                }else{
                    if(this.pickupingCheck.right.qty_sum <= this.pickupingCheck.storeInfo.detail[i].qty){
                        this.pickupingCheck.storeInfo.detail[i]['inputValue'] = this.pickupingCheck.right.qty_sum/this.pickupingCheck.proInfo.prochar_qty
                        this.pickupingInputMaxPcs = 0
                    }else{
                        this.pickupingCheck.storeInfo.detail[i]['inputValue'] = this.pickupingCheck.storeInfo.detail[i].qty/this.pickupingCheck.proInfo.prochar_qty
                        this.pickupingInputMaxPcs = this.pickupingCheck.right.qty_sum - this.pickupingCheck.storeInfo.detail[i].qty
                    }    
                }
            }

                console.log(this.pickupingCheck)
        },
        // 提货中完成tab
        savePickupingFulfil(type,key=-1){
            if(type == 'view'){
                this.clickCheck(key,'pickuping')
            }else if(type == 'save'){
                console.log(this.pickupingCheck)
                if(this.pickupingCheck.right.destination.length == 0){
                    var alertData = {status:2,msg:'提货失败，未设置提货目的地，请前往仓库设置，设置仓库区域类型：中转区/散货区/包装部'}
                    this.point(alertData,'#pickup_finishModal',2)
                }else if(this.pickupingCheck.storeInfo.qty < this.pickupingCheck.right.qty_sum){
                    var alertData = {status:2,msg:'提货失败，本地仓库没有/缺少当前产品库存'}
                    this.point(alertData,'#pickup_finishModal',2)
                }else{
                    
                    for (var i = 0; i < this.pickupingCheck.right.detail.length; i++) {

                        if(this.pickupingCheck.right.detail[i].area_id == 0){
                            var alertData = {status:2,msg:'订单号：'+this.formatUnit(this.pickupingCheck.right.detail[i].sale_id,'id')+',未选择目的地'}
                            this.point(alertData,'#pickup_finishModal',2)
                            return false
                        }
                    }

                    this.isdisabled = true
                    var data = this.pickupingCheck
                    $.ajax({
                        url:'/admin/saledocker/savePickupingFulfil',
                        type:'post',
                        data,
                        success:(data)=>{
                            this.isdisabled = false
                            data = JSON.parse(data)
                            this.point(data,'#pickup_finishModal')
                            if(data.status == 1){
                                this.point(data,'#mainalert',2)
                                this.getdata()
                            } 
                        }
                    })     
                }
                
            }
            
        },
        // 提货完成 设置input运行输入最大值
        setInputMax(placeMax,inputId,key){
            // 设置系统最大值
            this.pickupingInputMaxPcs = this.pickupingCheck.right.qty_sum/this.pickupingCheck.proInfo.prochar_qty
            // 判断input最大值和当前输入的input最大值 对比
            //  系统最大值 大于 input允许最大值的话，再判断input输入值和input允许最大值
            if(this.pickupingInputMaxPcs > placeMax){   

                // 当input输入值，大于inout允许最大值，小于系统最大值， 则input-value为input允许最大值，并且设置系统最大值为系统最大值减去input允许最大值
                if( $('#'+inputId+key).val() > placeMax ){
                    this.pickupingCheck.storeInfo.detail[key]['inputValue'] = placeMax
                    $('#'+inputId+key).val(placeMax)
                    this.pickupingInputMaxPcs = this.pickupingInputMaxPcs - placeMax

                //  当input输入值，小于inout允许最大值，则必然小于系统最大值， 则input-value为input输入值最大值，并且重新设置系统最大值为系统最大值减去input输入值
                }else if($('#'+inputId+key).val() < placeMax ){
                    this.pickupingCheck.storeInfo.detail[key]['inputValue'] = $('#'+inputId+key).val()
                    $('#'+inputId+key).val($('#'+inputId+key).val())
                    this.pickupingInputMaxPcs = this.pickupingInputMaxPcs - $('#'+inputId+key).val()
                }   
            // 当系统最大值 小于等于 input允许最大值
            }else if( this.pickupingInputMaxPcs <= placeMax ){
                // 当input输入值 大于/等于系统最大值，input的value值为系统最大值，且重新设置系统最大值为0
                if($('#'+inputId+key).val() >= this.pickupingInputMaxPcs){
                    this.pickupingCheck.storeInfo.detail[key]['inputValue'] = this.pickupingInputMaxPcs
                    $('#'+inputId+key).val(this.pickupingInputMaxPcs)
                    this.pickupingInputMaxPcs = 0

                // 当input输入值 小于系统最大值，input的value值为inout输入值，且重新设置系统最大值为系统最大值减去inout输入值
                }else{
                    this.pickupingCheck.storeInfo.detail[key]['inputValue'] = $('#'+inputId+key).val()
                    $('#'+inputId+key).val($('#'+inputId+key).val())
                    this.pickupingInputMaxPcs = this.pickupingInputMaxPcs - $('#'+inputId+key).val()
                }
            }
            

            // 当其中一个inout 改变了值，其他仓库的place input 值跟着变动
            for (var i = 0; i < this.pickupingCheck.storeInfo.detail.length; i++) {

                if(this.pickupingInputMaxPcs > 0 && i!=key){

                    if( this.pickupingCheck.storeInfo.detail[i]['qty']/this.pickupingCheck.proInfo.prochar_qty >= this.pickupingInputMaxPcs ){
                        this.pickupingCheck.storeInfo.detail[i]['inputValue'] = this.pickupingInputMaxPcs
                        this.pickupingInputMaxPcs = 0

                    }else{
                        this.pickupingCheck.storeInfo.detail[i]['inputValue'] = this.pickupingCheck.storeInfo.detail[i]['qty']/this.pickupingCheck.proInfo.prochar_qty
                        this.pickupingInputMaxPcs = this.pickupingInputMaxPcs - this.pickupingCheck.storeInfo.detail[i]['qty']/this.pickupingCheck.proInfo.prochar_qty 
                    }

                }else if( this.pickupingInputMaxPcs <= 0 && i!=key ){
                    this.pickupingCheck.storeInfo.detail[i]['inputValue'] = 0
                    this.pickupingInputMaxPcs = 0
                }
                // console.log(this.pickupingInputMaxPcs)
            }

            var data = this.pickupingCheck.storeInfo.detail
            this.pickupingCheck.storeInfo.detail = []
            this.pickupingCheck.storeInfo.detail = data
        },
        // 提货 -- 缺货
        pickupMissing(detailData,type='get'){
            if(type == 'get'){
                this.pickupMissingData = detailData
                var data = {
                    sale_id:detailData.sale_id,
                    pro_id:detailData.pro_id,
                    prochar_id:detailData.prochar_id,
                }
                this.pickupMissingData.saleList = []
            }else if(type == 'save'){
                var data = this.pickupMissingData
            }
            this.isdisabled = true
            $.ajax({
                url:'/admin/saledocker/pickupMissing?requestType='+type,
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    if(type == 'get'){
                        this.pickupMissingData.saleList = data.list
                        this.pickupMissingData.qtySum = data.qtySum
                        var result = this.pickupMissingData
                        this.pickupMissingData = {}
                        this.pickupMissingData = result
                        console.log(this.pickupMissingData)
                    }else{
                        this.point(data,'#pickup_lostModal')
                        if(data.status == 1){
                            this.point(data,'#mainalert',2)
                            this.getdata()
                        }
                    }
                    this.isdisabled = false
                }
            })


        },
        // 取消完成
        finishCancel(pickup_id,index){
            // console.log(this.finish[index])
            this.isdisabled = true
            $.ajax({
                url:'/admin/saledocker/finishCancel',
                type:'post',
                data:{id:pickup_id},
                success:(data)=>{
                    data = JSON.parse(data)
                    console.log(data)
                    if(data.status == 1) this.finish.splice(index,1)
                    this.point(data,'#mainalert',0)
                    this.isdisabled = false
                }
            }) 
        },
        //  取回
        getBack(key,pickup_id){
            this.clickCheck(key,'cancel')
            $.ajax({
                url:'/admin/saledocker/getBack',
                type:'post',
                data:{id:pickup_id,requestType:'getPlace'},
                success:(data)=>{
                    data = JSON.parse(data)
                    this.placeList = data
                }
            }) 

        },
        saveCancel(){
            var data = this.pickupingCheck.right
            $.ajax({
                url:'/admin/saledocker/getBack?requestType=save',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.placeList = data
                }
            }) 
        },
        




        onlyMeWork(){
            this.status = !this.status
            console.log(this.status)
            this.getdata()
        },
        point(data,modelId,type=1){
            change_mes_type(modelId+' .alert',data.status)
            if(data.status == 1){
                if(type == 1){
                    if(modelId != '.container-fluid') $(modelId).modal('toggle')
                }
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }else{
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }
        },
        //设置popper组件的配置
        setpopper(e){
            $(e.target).popover({
                trigger: 'hover focus manual',
                placement: ($(e.target).attr("placement") == undefined) ? 'left' : $(e.target).attr("placement"),
                //placement:'left',
                title: $(e.target).attr("title"),
                html: 'true',
                content: $(e.target).find(".content").html(),
                animation: false
            });
        },
        getList(requestType,e){
            this.requestType = requestType
            this.getdata()
        },
        navPageNum(){
            $.ajax({
                url:'/admin/saledocker/getNavPageNum',
                type:'get',
                success:(data)=>{
                    data = JSON.parse(data)
                    this.navPromptNum = data
                }
            })
        },
        getBackInputMAx(max,key,$event){
            if(event.target.value > max) event.target.value = max

            this.pickupingCheck.right.detail[key]['getBackQty'] = ''
            this.pickupingCheck.right.detail[key]['getBackQty'] = event.target.value

        },

    },
    mounted(){
        this.requestUrl = "/admin/saledocker/list?request=requestAjax";
        this.getdata()
        this.admin_id = $('#admin_id').val()
    }
})